package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.BagUtils;
import org.apache.commons.collections4.Get;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工管理")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private HttpServletRequest request;




    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @ApiOperation(value = "登录接口")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }



    /**
     * 退出
     *
     * @return
     */
    @ApiOperation(value = "退出接口")
    @PostMapping("/logout")
    public Result<String> logout() {
        log.info("退出接口");
         //修改状态码
        response.setStatus(401);

        String token = request.getHeader("Token");
        log.info("token:"+ token);
        return Result.success();
    }


    /**
     * 添加员工
     * @param employeeDTO
     * @return
     */

    @ApiOperation( "添加员工")
    @PostMapping
    public Result insertEmp(@RequestBody EmployeeDTO employeeDTO){
        log.info("新增员工参数："+ employeeDTO.toString());
        employeeService.insertEmp(employeeDTO);
        return  Result.success();
    }

    /**
     *根据id查员工
     */
    @ApiOperation("根据员工id查询")
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable Long id) {
        log.info("id :"+ id );
        return employeeService.getById(id);
    }


    /**
     * 分页查询
     * @param pageQueryDTO
     * @return
     */
    @ApiOperation("员工分页查询")
    @GetMapping("/page")
    public Result pageQuery(EmployeePageQueryDTO pageQueryDTO){
        log.info("分页参数："+pageQueryDTO);
        PageResult pageResult = employeeService.pageQuery(pageQueryDTO);
        return Result.success(pageResult);
        //return Result.success();
    }


    @ApiOperation("修改员工的状态")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable("status") Integer status,@RequestParam("id") Long empId){
        System.out.println("进来了！！！");
        log.info("状态："+ status);
        log.info("员工id："+ empId);
        return employeeService.updateStatus(status, empId);
    }

    @ApiOperation("编辑员工信息")
    @PutMapping
    public Result putEmpInfo(@RequestBody EmployeeDTO employeeDTO){
        log.info("员工新信息："+ employeeDTO);
        employeeService.putEmpInfo(employeeDTO);
        return Result.success();

    }

    @ApiOperation("修改密码")
    @PutMapping("/editPassword")
    public Result putPwd(@RequestBody PasswordEditDTO passwordEditDTO){
        log.info("新密码："+ passwordEditDTO);
        employeeService.putPwd(passwordEditDTO);
        return Result.success();
    }



}
